package Physics;

///////////////////////////////////////////
//Explosion
///////////////////////////////////////////
//Applies a force on all nearby objects
//based on their distance from the center
//of the explosion
///////////////////////////////////////////
public class Explosion extends ForceGenerator
{
	// tracks how long the explosion has been in operation
	double m_timePassed;
	
	// the location of the explosion
	Vector3 m_detonation;
	
	// speed that the wave is traveling, which is related
	// to the thickness based on the following relationship
	// thickness >= speed * minimum frame duration
	//double shockwaveSpeed;
	
	// max radius of the explosion
	double m_radius;
	
	// the shockwave applies its force over a range of distances
	// which controls thickness. Faster waves require larger 
	// thicknesses
	//double shockwaveThickness;
	
	// this is the force that is applied at the very center of the
	// concussion wave on an object that is stationary. Objects that
	// that are in front, or behind, the wavefront, or that are 
	// already moving outward, get proportionally less force. Objects
	// moving in toward the center get proportionally more force.
	double m_peakConcussionForce;
	
	// the length of time that the concussion wave is active.
	// As the wave nears this, the forces it applies are reduced.
	//double concussionDuration;
	
	/*Explosion(  Vector3 &detonation,
	double &speed,
	double &thickness,
	double &peakForce,
	double &duration);*/
	Explosion(Vector3 detonation, double radius, double peakForce)
	{
		m_detonation = detonation;
		m_radius = radius;
		m_peakConcussionForce = peakForce;			
	}
	
	public void updateForce(RigidBody body, double dt)
	{
		if (!body.m_activated)
			return;
		
		Vector3 pos = body.getPosition();
		Vector3 distance = pos.subtract(m_detonation);
		double magSqr = distance.squareMagnitude();
		if (magSqr > m_radius * m_radius)
			return;
		
		Vector3 force = distance;
		force.normalize();
		
		force.multiplyAndSet((float)m_peakConcussionForce / (float)magSqr);
		//float mag = force.magnitude();
		body.applyForce(force.multiply(1.0f / (float)dt));
	}
}
